import { request, useUserStore, clearEmpty, vueMetaEnv } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  UserReq,
  UserApi,
  UserPage,
  UserList,
  UserCreateReq,
  UserUpdateReq,
  UserKey,
  UserModel,
  UserKeyList,
  UserEnableKey,
  UserImportReq,
  UserExportReq,
  UserResetPwdReq,
  OpensKey,
  UserLockReq,
  UserEnableKeyList,
  UserApprovedReq,
  DirectoryModel,
  UserOpenList,
  UserOpenModel,
  SelectUsersList,
  SelectUsersReq,
  UserChoosePage,
  UserPlatformAddReq,
} from "./types";
import qs from "qs";

class UserApiImpl implements UserApi {
  getKey(model: UserModel): UserKey {
    return {
      userId: model.userId,
    } as UserKey;
  }
  /**
   * 获取分页信息
   *
   * @param queryParams 分页查询参数
   * @returns 返回请求结果
   */
  findPage(query: UserReq): AxiosPromise<UserPage> {
    query.tenant = query.tenant || useUserStore().user.tenant;
    if (query) {
      query.hasUserStatus = query.userStatus != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/page",
      method: "get",
      params: query,
    });
  }

  /**
   * 获取分页信息
   *
   * @param queryParams 分页查询参数
   * @returns 返回请求结果
   */
  findPageOrganTenant(query: UserReq): AxiosPromise<UserPage> {
    query.tenant = vueMetaEnv.APP_TENANT;
    if (query) {
      query.hasUserStatus = query.userStatus != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/page",
      method: "get",
      params: query,
    });
  }

  findList(query: UserReq): AxiosPromise<UserList> {
    query.tenant = query.tenant || useUserStore().user.tenant;
    if (query) {
      query.hasUserStatus = query.userStatus != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/list",
      method: "get",
      params: query,
    });
  }

  // 查询用户
  findById(query: UserKey): AxiosPromise<UserModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/id",
      method: "get",
      params: query,
    });
  }
  // 查询平台用户列表
  findPlatformUserList(query: UserKey): AxiosPromise<UserList> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/list_user",
      method: "get",
      params: query,
    });
  }
  /**
   * 删除用户
   *
   * @param userId 用户ID
   * @returns 返回请求结果
   */
  delete(query: UserKey): AxiosPromise<UserModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/id",
      method: "delete",
      params: query,
    });
  }

  /**
   * 批量删除用户
   *
   * @param ids 用户ID数组
   * @returns 返回请求结果
   */
  batchDelete(keys: UserKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.userId);
    const data: UserKeyList = {
      tenant: useUserStore().user.tenant,
      userId: ids,
    } as UserKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/user/batch",
      method: "put",
      data: data,
    });
  }

  /**
   * 添加用户
   *
   * @param data 用户数据
   * @returns 返回请求结果
   */
  create(data: UserCreateReq): AxiosPromise<UserModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/user",
      method: "post",
      data: data,
    });
  }

  // 添加平台用户
  createUserAdd(data: UserPlatformAddReq): AxiosPromise<UserModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/organ/user/add",
      method: "post",
      data: data,
    });
  }

  /**
   * 编辑用户信息
   *
   * @param data 用户数据
   * @returns 返回请求结果
   */
  update(data: UserUpdateReq): AxiosPromise<UserModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/user",
      method: "patch",
      data: data,
    });
  }

  /**
   * 启用禁用用户API
   *
   * @param userId 用户ID
   * @returns 返回请求结果
   */
  enable(key: UserKey, enable: boolean): AxiosPromise<boolean> {
    const data: UserEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enabled = enable;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/user/enable",
      method: "put",
      data: data,
    });
  }

  /**
   * 批量启用禁用用户API
   *
   * @param data 批量启用用户的数据
   * @returns 返回请求结果
   */
  batchEnable(key: UserKeyList, enable: boolean): AxiosPromise<number> {
    const data: UserEnableKeyList = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enabled = enable;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/user/batch_enable",
      method: "put",
      data: data,
    });
  }

  /**
   * 下载用户模板
   *
   * @returns 返回请求的 Promise 对象
   */

  downloadTemplate(): AxiosPromise {
    return request({
      url: "/v1/rbac/user/template.xlsx",
      method: "get",
      responseType: "blob",
      params: {
        tenant: useUserStore().user.tenant,
      },
    });
  }

  /**
   * 导入用户
   *
   * @param data 用户数据
   * @returns 返回请求结果
   */

  importExcel(data: UserImportReq): AxiosPromise<string> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/rbac/user/import",
      method: "patch",
      data: data,
    });
  }

  /**
   * 导出用户Excel文件
   *
   * @param queryParams 查询参数，可选
   * @returns 返回一个Promise对象，用于处理响应结果
   */
  exportExcel(queryParams: UserExportReq): AxiosPromise {
    clearEmpty(queryParams);
    const query = qs.stringify(queryParams, {
      arrayFormat: "brackets",
      allowDots: true,
    });
    return request({
      url:
        "/v1/rbac/user/" + useUserStore().user.tenant + "/users.xlsx?" + query,
      method: "get",
      responseType: "blob",
    });
  }

  /**
   * 导出用户PDF文件
   *
   * @param queryParams 查询参数，可选
   * @returns 返回请求结果
   */

  exportPdf(queryParams: UserExportReq): AxiosPromise {
    clearEmpty(queryParams);
    const query = qs.stringify(queryParams, {
      arrayFormat: "brackets",
      allowDots: true,
    });
    return request({
      url:
        "/v1/rbac/user/" + useUserStore().user.tenant + "/users.pdf?" + query,
      method: "get",
      responseType: "blob",
    });
  }

  /**
   * 用户锁定
   *
   * @param data 锁定用户所需的数据
   * @returns 返回请求结果
   */
  userUnlock(data: UserLockReq): AxiosPromise<number> {
    return request({
      url: "/v1/rbac/user/lock",
      method: "put",
      data: data,
    });
  }

  /**
   * 重置密码
   *
   * @param data 包含用户名和新密码的对象
   * @returns 返回请求结果
   */
  resetpwd(data: UserResetPwdReq): AxiosPromise<boolean> {
    data.tenant = useUserStore().user.tenant;
    return request({
      url: "/v1/rbac/user/resetpwd",
      method: "put",
      data: data,
    });
  }

  /**
   * 用户审核API
   *
   * @param data 请求数据
   * @returns 返回请求结果
   */
  approveUserApi(data: UserApprovedReq): AxiosPromise<number> {
    return request({
      url: "/v1/rbac/user/approved",
      method: "put",
      data: data,
    });
  }

  /**
   * 获取目录API
   *
   * @returns 返回一个请求对象
   */
  getDirectoryApi(): AxiosPromise<DirectoryModel> {
    return request({
      url:
        "/v1/rbac/user/select_directory_id?tenant=" +
        useUserStore().user.tenant,
      method: "get",
    });
  }

  /**
   * 判断邮箱/手机号/用户名是否存在
   *
   * @param userId 用户ID
   * @param userName 邮箱/手机号/用户名
   * @returns 返回请求结果
   */
  existUser(userId: string, userName: string): AxiosPromise<boolean> {
    const query = {
      tenant: useUserStore().user.tenant,
      userId: userId,
      userName: userName,
      directoryId: "jmash",
    };
    return request({
      url: "/v1/rbac/user/exist",
      method: "get",
      params: query,
    });
  }

  /**
   * 第三方登录列表
   *
   * @param userId 用户ID
   * @returns 返回请求结果
   */
  openList(query: UserKey): AxiosPromise<UserOpenList> {
    query.tenant = query.tenant || useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/opens/list",
      method: "get",
      params: query,
    });
  }

  /**
   * 解绑第三方登录
   *
   * @param query 请求参数
   * @returns 返回请求结果
   */
  unBindOpens(query: OpensKey): AxiosPromise<UserOpenModel> {
    return request({
      url: "/v1/rbac/opens/id",
      method: "delete",
      params: query,
    });
  }
  /**
   * 获取下拉用户列表
   * @param query 查询参数对象，包含分页和筛选条件
   * @returns 返回一个AxiosPromise，其中包含用户列表数据
   */
  getSelectUsersList(query: SelectUsersReq): AxiosPromise<SelectUsersList> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/list_user",
      method: "get",
      params: query,
    });
  }

  /**
   * 根据部门选择人员分页信息
   *
   * @param queryParams 分页查询参数
   * @returns 返回请求结果
   */
  userPage(query: UserReq): AxiosPromise<UserChoosePage> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/rbac/user/page_user",
      method: "get",
      params: query,
    });
  }
}
const userApi = new UserApiImpl();
export { userApi };

/**
 * 获取用户ID
 *
 * @param userId 用户ID
 * @returns 返回请求结果
 */
export function userId(userId: string): AxiosPromise {
  const query = {
    tenant: useUserStore().user.tenant,
    userId: userId,
  };
  return request({
    url: "/v1/rbac/user/id",
    method: "get",
    params: query,
  });
}
